package com.pj.wisdomlogistics.ui.activity;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.entity.TakeoutDetailInfo;
import com.pj.wisdomlogistics.mvp.contract.TakeoutDetailContract;
import com.pj.wisdomlogistics.mvp.frame.MvpActivity;
import com.pj.wisdomlogistics.mvp.frame.base.BaseView;
import com.pj.wisdomlogistics.mvp.presenter.TakeoutDetailPresenter;
import com.pj.wisdomlogistics.other.EventCenter;
import com.pj.wisdomlogistics.ui.adapter.TakeoutDetailAdapter;
import com.pj.wisdomlogistics.utils.ActivityManagerUtil;
import com.pj.wisdomlogistics.utils.CustomToast;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.OnClick;

public class TakeoutDetailActivity extends MvpActivity<TakeoutDetailPresenter> implements TakeoutDetailContract.View {

    @BindView(R.id.tv_quhuoma)
    TextView mTvNumber;
    @BindView(R.id.rv_order)
    RecyclerView mRvTakeaway;
    @BindView(R.id.tv_order_price)
    TextView mTvTotalPrice;
    @BindView(R.id.bt_get)
    Button mBtGet;
    @BindView(R.id.bt_cancel)
    Button mBtCancel;
    @BindView(R.id.tv_order_state)
    TextView mTvOrderState;
    @BindView(R.id.iv_order_state)
    ImageView mIvOrderState;
    @BindView(R.id.tv_order_time)
    TextView mTvOrderTime;
    @BindView(R.id.tv_order_address)
    TextView mTvOrderAddress;
    @BindView(R.id.tv_order_num)
    TextView mTvOrderNum;

    private Calendar mCalendar;
    private int hour;
    private int id;

    private int isToday; //1 今天

    private TakeoutDetailAdapter mAdapter;

    @Override
    public void initView() {
        ultimateBar.setColorBar(ContextCompat.getColor(this , R.color.text_blue));

        id = getIntent().getIntExtra(Cons.TakeoutMeal.TAKEOUT_ID, -1);

        mCalendar = Calendar.getInstance();
        hour = mCalendar.get(Calendar.HOUR_OF_DAY);

        mRvTakeaway.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mAdapter = new TakeoutDetailAdapter(this);
        mRvTakeaway.setAdapter(mAdapter);

        mPresenter.findFoodApplyDetail(id);
        showProgressDialog();
    }

    @Override
    public void initData() {

    }

    @Override
    public int getContentView() {
        return R.layout.activity_takeout_detail;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }

    @OnClick({R.id.iv_back, R.id.bt_get, R.id.bt_cancel})
    public void onViewClicked(View mView) {
        switch (mView.getId()) {
            case R.id.iv_back:
                ActivityManagerUtil.create().finishActivity();
                break;
            case R.id.bt_get:
                mPresenter.foodApplyFinish(id);
                break;
            case R.id.bt_cancel:
                mPresenter.cancelFoodApply(id);
                break;
        }
    }

    @Override
    public void showError(String errorMsg) {
        Toast.makeText(this, errorMsg, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void showFinishSuccess() {
        dismissProgressDialog();
        mTvNumber.setVisibility(View.VISIBLE);
        if(isToday == 1){
            mTvNumber.setBackgroundResource(R.drawable.quhuoma_lan);
        }else{
            mTvNumber.setBackgroundResource(R.drawable.quhuoma_hui);
        }
        mBtGet.setVisibility(View.GONE);
        mBtCancel.setVisibility(View.GONE);
        EventBus.getDefault().post(new EventCenter<>(Cons.Other.MY_APPLY));

        mTvOrderState.setText("订单已取码");
        mIvOrderState.setImageResource(R.drawable.wancheng);
    }

    @Override
    public void showCancelSuccess() {
        dismissProgressDialog();
        CustomToast.showToast(this, "撤销成功", 1000);
        ActivityManagerUtil.create().finishActivity();
        EventBus.getDefault().post(new EventCenter<>(Cons.Other.MY_APPLY));

        mTvOrderState.setText("订单已撤销");
        mIvOrderState.setImageResource(R.drawable.chexiao);
    }

    @Override
    public void showDetailSuccess(TakeoutDetailInfo mInfo) {
        dismissProgressDialog();

        isToday = mInfo.getIsToday();
        // 待付款
        if (hour < 12 && mInfo.getState() == 0) {
            mBtGet.setVisibility(View.GONE);
            mBtCancel.setVisibility(View.GONE);
        }
        if ((hour < 16 && hour >= 12) && mInfo.getState() == 0) {
            mBtGet.setBackgroundResource(R.drawable.takeout_bg_un);
            mBtGet.setEnabled(false);
        }
        if (hour >= 16 && mInfo.getState() == 0) {
            mBtCancel.setBackgroundResource(R.drawable.takeout_bg_un);
            mBtCancel.setEnabled(false);
        }

        if (mInfo.getState() == 1) {
            mBtGet.setVisibility(View.GONE);
            mBtCancel.setVisibility(View.GONE);
            mTvNumber.setVisibility(View.VISIBLE);
            if(isToday == 1){
                mTvNumber.setBackgroundResource(R.drawable.quhuoma_lan);
            }else{
                mTvNumber.setBackgroundResource(R.drawable.quhuoma_hui);
            }
        }

        if (mInfo.getState() == 2) {
            mBtGet.setVisibility(View.GONE);
            mBtCancel.setVisibility(View.GONE);
            mTvNumber.setVisibility(View.GONE);
        }


        switch (mInfo.getState()){
            case 0:
                mTvOrderState.setText("订单待付款");
                mIvOrderState.setImageResource(R.drawable.fukuan);
                break;
            case 1:
                mTvOrderState.setText("订单已取码");
                mIvOrderState.setImageResource(R.drawable.wancheng);
                break;
            case 2:
                mTvOrderState.setText("订单已撤销");
                mIvOrderState.setImageResource(R.drawable.chexiao);
                break;
        }

        mTvNumber.setText(mInfo.getOrderNum() + "");
        mTvTotalPrice.setText(mInfo.getTotalPrice() + "");
        mTvOrderTime.setText(mInfo.getCreateTime());
        mTvOrderAddress.setText(mInfo.getPlace() == 1 ? "政府大楼" : "双和园");
        mTvOrderNum.setText(mInfo.getOrderID());
        mAdapter.setList(mInfo.getDetailList());

    }


    @Override
    protected BaseView getBaseView() {
        return this;
    }

}
